package com.itheima.controller;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Article;
import com.itheima.pojo.Result;
import com.itheima.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    /**
     * 新增/更新文章
     */
    @PostMapping
    public Result addArticle(@RequestBody @Validated(Article.Add.class) Article article){
        articleService.addArticle(article);
        return Result.success("添加成功");
    }

    /**
     * 更新文章
     */
    @PutMapping
    public Result updateArticle(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.updateArticle(article);
        return Result.success("修改成功");
    }

    /**
     * 根据id查询文章
     */
    @GetMapping("/{id}")
    public Result<Article> getArticleById(@PathVariable("id") int Id) {

        Article article = articleService.getArticleById(Id);
        return Result.success(article);
    }

    /**
     * 根据id删除文章
     */
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable("id") int Id) {
        articleService.deleteArticle(Id);
        return Result.success("删除成功");
    }


    /**
     * 根据id删除文章集合
     */
    @DeleteMapping("/{ids}")
    public Result deleteArticle2(@PathVariable("ids") int ids) {
        articleService.deleteArticle(ids);
        return Result.success("删除成功");
    }

    /**
     * 跟新文章
     */
    @DeleteMapping("/{id}")
    public Result update(@PathVariable("id") int id) {
        return Result.success();
    }

    /**
     * 带条件的分页查询
     */
    @GetMapping
    public Result<PageInfo<Article>> getArticlePage(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                                @RequestParam(value = "categoryId",required = false) Integer categoryId,
                                                @RequestParam(value = "state",required = false) String state) {

        PageInfo<Article> page = articleService.findPage(pageNum, pageSize, categoryId, state);
        return Result.success(page);
    }



}
